// SlagCar/DailyCheckDetl/DailyCheckDetl.js
import {config} from "../../utils/config";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    driver_car_name: '',
    pageTitle: '',
    tableLayout: [
     // 240119 yan 注释 第一版一日三检项目
     /* {
        title: '三角木',
        fields: [
          {label: '状况',field_id: 'sjmzhuangkuang',type: 'picklist',option:"[{\"label\":\"正常\",\"isDefault\":true},{\"label\":\"隐患\",\"isDefault\":false}]",is_null: false},
          {label: '处理',field_id: 'sjmchuli',type: 'textarea',},
        ]
      },
      {
        title: '灭火器',
        fields: [
          {label: '状况',field_id: 'mhqzhuangkuang',type: 'picklist',option:"[{\"label\":\"正常\",\"isDefault\":true},{\"label\":\"隐患\",\"isDefault\":false}]",is_null: false},
          {label: '处理',field_id: 'mhqchuli',type: 'textarea',},
        ]
      },
      {
        title: '行驶证、营运证',
        fields: [
          {label: '状况',field_id: 'xszyyzzhuangkuang',type: 'picklist',option:"[{\"label\":\"正常\",\"isDefault\":true},{\"label\":\"隐患\",\"isDefault\":false}]",is_null: false},
          {label: '处理',field_id: 'zszyyzchuli',type: 'textarea',},
        ]
      },
      {
        title: '危险标记',
        fields: [
          {label: '状况',field_id: 'wxbjzhuangkuang',type: 'picklist',option:"[{\"label\":\"正常\",\"isDefault\":true},{\"label\":\"隐患\",\"isDefault\":false}]",is_null: false},
          {label: '处理',field_id: 'wxbjchuli',type: 'textarea',},
        ]
      },
      {
        title: '制动系统',
        fields: [
          {label: '状况',field_id: 'zdxtzhuangkuang',type: 'picklist',option:"[{\"label\":\"正常\",\"isDefault\":true},{\"label\":\"隐患\",\"isDefault\":false}]",is_null: false},
          {label: '处理',field_id: 'zdxtchuli',type: 'textarea',},
        ]
      },
      {
        title: '全车灯光',
        fields: [
          {label: '状况',field_id: 'qcdgzhuangkuang',type: 'picklist',option:"[{\"label\":\"正常\",\"isDefault\":true},{\"label\":\"隐患\",\"isDefault\":false}]",is_null: false},
          {label: '处理',field_id: 'qcdgchuli',type: 'textarea',},
        ]
      },
      {
        title: '轮胎',
        fields: [
          {label: '状况',field_id: 'ltzhuangkuang',type: 'picklist',option:"[{\"label\":\"正常\",\"isDefault\":true},{\"label\":\"隐患\",\"isDefault\":false}]",is_null: false},
          {label: '处理',field_id: 'ltchuli',type: 'textarea',},
        ]
      },
      {
        title: '转向系统',
        fields: [
          {label: '状况',field_id: 'zxxtzhuangkuang',type: 'picklist',option:"[{\"label\":\"正常\",\"isDefault\":true},{\"label\":\"隐患\",\"isDefault\":false}]",is_null: false},
          {label: '处理',field_id: 'zxxtchuli',type: 'textarea',},
        ]
      },
      {
        title: '行车记录仪',
        fields: [
          {label: '状况',field_id: 'zcjlyzhuangkuang',type: 'picklist',option:"[{\"label\":\"正常\",\"isDefault\":true},{\"label\":\"隐患\",\"isDefault\":false}]",is_null: false},
          {label: '处理',field_id: 'xcjlychuli',type: 'textarea',},
        ]
      },
      {
        title: 'GPS',
        fields: [
          {label: '状况',field_id: 'gpszhuangkuang',type: 'picklist',option:"[{\"label\":\"正常\",\"isDefault\":true},{\"label\":\"隐患\",\"isDefault\":false}]",is_null: false},
          {label: '处理',field_id: 'gpschuli',type: 'textarea',},
        ]
      },
      {
        title: '智能视频监控装置',
        fields: [
          {label: '状况',field_id: 'znspjkzhuangkuang',type: 'picklist',option:"[{\"label\":\"正常\",\"isDefault\":true},{\"label\":\"隐患\",\"isDefault\":false}]",is_null: false},
          {label: '处理',field_id: 'znspjkchuli',type: 'textarea',},
        ]
      },*/
    ],
    //字段对象--固定字段显示注释
    /*fieldsObj: {
      jianchacheshen: {label: '检查、清洁车身',field_id: 'jianchacheshen',type: 'picklist',option:"[{\"label\":\"内外整洁车窗完好\",\"isDefault\":false},{\"label\":\"未清洁\",\"isDefault\":false},{\"label\":\"车窗缺失\",\"isDefault\":false}]",is_null: false},
      cheshenzhaopian: {label: '车身照片（内部一张、外部一张）',field_id: 'cheshenzhaopian',type: 'picture',allowUpload: false,compress: true,watermark: () => this.getWatermark()},
      jianchahoushijing: {label: '检查后视镜,调整后视镜角度',field_id: 'jianchahoushijing',type: 'picklist',is_null: false,option: "[{\"label\":\"正常\",\"isDefault\":false},{\"label\":\"左边坏\",\"isDefault\":false},{\"label\":\"右边坏\",\"isDefault\":false},{\"label\":\"左右都坏\",\"isDefault\":false}]"},
      houshijingzhaopian: {label: '后视镜照片（左边一张、右边一张）',field_id: 'houshijingzhaopian',type: 'picture',allowUpload: false,compress: true,watermark: () => this.getWatermark()},
      jianchamiehuoqi: {label: '检查灭火器',field_id: 'jianchamiehuoqi',type: 'picklist',is_null: false,option: "[{\"label\":\"有效且放在指定位置\",\"isDefault\":false},{\"label\":\"无效\",\"isDefault\":false}]"},
      miehuoqizhaopian: {label: '灭火器照片',field_id: 'miehuoqizhaopian',type: 'picture',allowUpload: false,compress: true,watermark: () => this.getWatermark()},
      kecheanquanchui: {label: '客车安全锤',field_id: 'kecheanquanchui',type: 'picklist',is_null: false,option: "[{\"label\":\"数量及位置符合\",\"isDefault\":false},{\"label\":\"缺少\",\"isDefault\":false},{\"label\":\"位置异常\",\"isDefault\":false}]"},
      anquanchuizhaopian: {label: '安全锤照片',field_id: 'anquanchuizhaopian',type: 'picture',allowUpload: false,compress: true,watermark: () => this.getWatermark()},
      jianchaanquandai: {label: '检查安全带',field_id: 'jianchaanquandai',type: 'picklist',is_null: false,option: "[{\"label\":\"正常\",\"isDefault\":false},{\"label\":\"松脱\",\"isDefault\":false},{\"label\":\"拉不动\",\"isDefault\":false}]"},
      anquandaizhaopian: {label: '安全带照片',field_id: 'anquandaizhaopian',type: 'picture',allowUpload: false,compress: true,watermark: () => this.getWatermark()},
      jianchaguashuiqi: {label: '检查风窗玻璃刮水器',field_id: 'jianchaguashuiqi',type: 'picklist',is_null: false,option: "[{\"label\":\"正常\",\"isDefault\":false},{\"label\":\"1档位异常\",\"isDefault\":false},{\"label\":\"2档位异常\",\"isDefault\":false},{\"label\":\"3档位异常\",\"isDefault\":false},{\"label\":\"4档位异常\",\"isDefault\":false},{\"label\":\"5档位异常\",\"isDefault\":false},{\"label\":\"多档位异常\",\"isDefault\":false}]"},
      jianchafadongji: {label: '检查发动机润滑油',field_id: 'jianchafadongji',type: 'picklist',is_null: false,option: "[{\"label\":\"正常\",\"isDefault\":false},{\"label\":\"少一格\",\"isDefault\":false},{\"label\":\"量不到\",\"isDefault\":false}]"},
      jianchalingqueye: {label: '检查冷却液液面高度',field_id: 'jianchalingqueye',type: 'picklist',is_null: false,option: "[{\"label\":\"正常\",\"isDefault\":false},{\"label\":\"水箱无水\",\"isDefault\":false},{\"label\":\"水箱生锈\",\"isDefault\":false}]"},
      jianchazhidongxitong: {label: '检查制动系统自检',field_id: 'jianchazhidongxitong',type: 'picklist',is_null: false,option: "[{\"label\":\"正常\",\"isDefault\":false},{\"label\":\"制动报警灯闪亮\",\"isDefault\":false}]"},
      zhidongxitongzhaopian: {label: '制动系统自检照片',field_id: 'zhidongxitongzhaopian',type: 'picture',allowUpload: false,compress: true,watermark: () => this.getWatermark()},
      jianchazhidongye: {label: '检查制动液液面高度,视情补给',field_id: 'jianchazhidongye',type: 'picklist',is_null: false,option: "[{\"label\":\"正常\",\"isDefault\":false},{\"label\":\"制动液不足\",\"isDefault\":false}]"},
      zhidongyezhaopian: {label: '制动液液面高度照片',field_id: 'zhidongyezhaopian',type: 'picture',allowUpload: false,compress: true,watermark: () => this.getWatermark()},
      jianchaxingchezhidong: {label: '检查行车制动',field_id: 'jianchaxingchezhidong',type: 'picklist',is_null: false,option: "[{\"label\":\"正常\",\"isDefault\":false},{\"label\":\"制动异常\",\"isDefault\":false}]"},
      jianchazhuchezhidong: {label: '检查驻车制动',field_id: 'jianchazhuchezhidong',type: 'picklist',is_null: false,option: "[{\"label\":\"正常\",\"isDefault\":false},{\"label\":\"制动异常\",\"isDefault\":false}]"},
      jianchaluntai: {label: '检查轮胎外观、气压',field_id: 'jianchaluntai',type: 'picklist',is_null: false,option: "[{\"label\":\"正常\",\"isDefault\":false},{\"label\":\"轮胎外观异常\",\"isDefault\":false},{\"label\":\"轮胎气压异常\",\"isDefault\":false},{\"label\":\"轮胎外观及气压异常\",\"isDefault\":false}]"},
      qianzuoluntaizhaopian: {label: '前左轮胎照片（至少三张，要拍到花纹）',field_id: 'qianzuoluntaizhaopian',type: 'picture',allowUpload: false,compress: true,watermark: () => this.getWatermark()},
      qianyouluntaizhaopian: {label: '前右轮胎照片（至少三张，要拍到花纹）',field_id: 'qianyouluntaizhaopian',type: 'picture',allowUpload: false,compress: true,watermark: () => this.getWatermark()},
      zhongqiaozuozhuluntaizhaopian: {label: '中桥左主轮胎照片（至少三张，要拍到花纹）',field_id: 'zhongqiaozuozhuluntaizhaopian',type: 'picture',allowUpload: false,compress: true,watermark: () => this.getWatermark()},
      zhongqiaozuobianluntaizhaopian: {label: '中桥左边轮胎照片（至少三张，要拍到花纹）',field_id: 'zhongqiaozuobianluntaizhaopian',type: 'picture',allowUpload: false,compress: true,watermark: () => this.getWatermark()},
      zhongqiaoyouzhuluntaizhaopian: {label: '中桥右主轮胎照片（至少三张，要拍到花纹）',field_id: 'zhongqiaoyouzhuluntaizhaopian',type: 'picture',allowUpload: false,compress: true,watermark: () => this.getWatermark()},
      zhongqiaoyoubianluntaizhaopian: {label: '中桥右边轮胎照片（至少三张，要拍到花纹）',field_id: 'zhongqiaoyoubianluntaizhaopian',type: 'picture',allowUpload: false,compress: true,watermark: () => this.getWatermark()},
      houqiaozuoluntaizhaopian: {label: '后桥左轮胎照片（至少三张，要拍到花纹）',field_id: 'houqiaozuoluntaizhaopian',type: 'picture',allowUpload: false,compress: true,watermark: () => this.getWatermark()},
      houqiaoyouluntaizhaopian: {label: '后桥右轮胎照片（至少三张，要拍到花纹）',field_id: 'houqiaoyouluntaizhaopian',type: 'picture',allowUpload: false,compress: true,watermark: () => this.getWatermark()},
      jianchachelunluoshuanluomu: {label: '检查车轮螺栓、螺母',field_id: 'jianchachelunluoshuanluomu',type: 'picklist',is_null: false,option: "[{\"label\":\"正常\",\"isDefault\":false},{\"label\":\"缺失\",\"isDefault\":false},{\"label\":\"松动\",\"isDefault\":false}]"},
      jianchaqianzhaodeng: {label: '检查前照灯',field_id: 'jianchaqianzhaodeng',type: 'picklist',is_null: false,option: "[{\"label\":\"正常\",\"isDefault\":false},{\"label\":\"肮脏\",\"isDefault\":false},{\"label\":\"左前坏\",\"isDefault\":false},{\"label\":\"右前坏\",\"isDefault\":false},{\"label\":\"两个坏\",\"isDefault\":false},{\"label\":\"远近切换异常\",\"isDefault\":false}]"},
      jianchaxinhaozhishi: {label: '检查信号指示装置',field_id: 'jianchaxinhaozhishi',type: 'picklist',is_null: false,option: "[{\"label\":\"正常\",\"isDefault\":false},{\"label\":\"肮脏\",\"isDefault\":false},{\"label\":\"转向灯异常\",\"isDefault\":false},{\"label\":\"制动灯异常\",\"isDefault\":false},{\"label\":\"示廓灯异常\",\"isDefault\":false},{\"label\":\"危险报警灯异常\",\"isDefault\":false},{\"label\":\"雾灯异常\",\"isDefault\":false},{\"label\":\"喇叭异常\",\"isDefault\":false},{\"label\":\"标志灯异常\",\"isDefault\":false},{\"label\":\"反射器异常\",\"isDefault\":false},{\"label\":\"多装置异常\",\"isDefault\":false}]"},
      jianchayibiao: {label: '检查仪表',field_id: 'jianchayibiao',type: 'picklist',is_null: false,option: "[{\"label\":\"正常\",\"isDefault\":false},{\"label\":\"车速里程表异常\",\"isDefault\":false},{\"label\":\"转速表异常\",\"isDefault\":false},{\"label\":\"机油压力表异常\",\"isDefault\":false},{\"label\":\"水温表异常\",\"isDefault\":false},{\"label\":\"燃油表异常\",\"isDefault\":false},{\"label\":\"充电表异常\",\"isDefault\":false}]"},
    },*/

    validFlag: false,
    validObj:{},
    formData:{},
    rowData:{},
    readonly: false,
    isEdit: false,

  //  下拉选择显示隐藏标识
    show: {},

    //签名弹窗
    showSignature: false,
    watermark:[], //签名水印

  },

  // 记录验证表单项
  validIpt(e) {
    let { validObj, } = this.data
    this.data.validObj = {
      ...validObj,
      ...e.detail,
    };
  },
  //验证数据
  validForm(validObj){
    return new Promise((resolve, reject) => {
      let valid = Object.values(validObj).includes(false) ? false : true;
      resolve(valid)
    })
  },

  //字段修改
  bindChange(e){
    let detail = e.detail,
      field = detail.field,
      id = field.field_id || field.fieldName,
      type = field.type,
      val = detail.val;

    if (type === 'num' || type === 'currency' || type === 'percent'){
      val = val ? val : null
    }
    if (type === 'picklist'){
      // this.setData({[`rowData.${id}`]: val})
    }
    //赋值
    this.setData({isEdit: true})
    this.setData({
      [`formData.${id}`]: val
    })
    this.setData({isEdit: false})

    // this.data.formData[id] = val;

  },
  //提交
  bindSubmit(){
    let {formData,rowData,tableLayout,entity,pageTitle} = this.data;

    this.data.validObj = {}
    this.setData({
      validFlag: !this.data.validFlag,
    });
    let validObj = this.data.validObj;

    this.validForm(validObj).then(async valid => {
      if(valid) { // 表单验证通过
        console.log('表单验证通过', formData);

        formData.row_id = rowData.row_id || 0;
        formData.jianchajieguo = '正常';
        try {
          tableLayout.forEach(item =>{
            item.fields.forEach(ite =>{
              if (ite.type === 'picklist' && formData[ite.field_id] !== '正常') {
                formData.jianchajieguo = '隐患'
                throw new Error('退出forEach循环！')
              }
            })
          })
        }catch (e) {}
        //签名
        let watermark = [
          {
            content: '$time$',
            font: '24px serif',
            position: {
              bottom: 15,
              left: 15
            }
          },
          {
            content: wx.getStorageSync('driver_car_name'),
            font: '22px serif',
            position: {
              bottom: 42,
              left: 15
            }
          },
          {
            content: `检查结果：${formData.jianchajieguo}`,
            font: '20px serif',
            position: {
              bottom: 70,
              left: 15
            }
          },
          {
            content: pageTitle,
            font: '20px serif',
            position: {
              bottom: 96,
              left: 15
            }
          }
        ]
        this.setData({watermark})
        this.setData({showSignature:true})

      } else {  // 表单验证不通过
        console.log('表单验证不通过')
        wx.$toast('必填项不能为空！')
      }
    })
  },
  //提交接口
  submitData(){
    let {formData,tableLayout,rowData,entity} = this.data;
    wx.showLoading({mask: true});
    let post = {
        table: {
          table_id: entity,
          entitys: [{
            details:[],
            entity: {
              ...formData,
            }
          }]
        }},
      url = `DynamicUpdate/dynamic_update_v3`;
    wx.$post(3,url,post).then(res =>{
      if (res.errcode != 0) return;
      let data = res.data.row[0],
        rid = data && data.entity[0];
      rid = rid && rid.row_id;
      // this.getRowData(rid);
      this.setData({readonly: true});
      wx.$toast('提交成功',1000).then(() =>{
        wx.navigateBack({ delta:1 });
      });


    })
  },
  //确认签名
  onSign({detail}){
    this.setData({showSignature:  false});
    wx.showLoading({mask: true});
    wx.$uploadReq(detail).then(res =>{
      if (res.response.errcode != 0) {
        wx.$toast(res.errmsg);
        return
      }
      wx.hideLoading();
      let data = res.data.row[0];
      this.data.formData.qianming = data.filename;

      this.submitData();
    })
  },
  onSignCancel(){
    this.setData({showSignature:  false});
  },

  //获取检查数据
  getRowData(rid){
    let {entity} = this.data,
      url = `DynamicQuery/query_dynamic_v3`,
      post = {
        "oby":`${entity}.row_id`,
        "table_id": entity,
        "page":1,
        "limit":30,
        "SearchWhere":{
          "filters":[
            {"CompareWith": `${entity}.row_id`,"Operator":"=","CompareValue": rid,"or":0},
          ],
          "type": 'and'
        },
      };
    wx.showLoading({mask: true});
    wx.$post(3,url,post).then(res =>{
      if (res.errcode != 0) return;
      wx.hideLoading();
      let data = res.data.row[0] || {};
      this.setData({formData: data,driver_car_name: data['chepaihaomayinyong;value'] || ''})
    })
  },


  prepare(type){
    let pageTitle = '',
      {fieldsObj} = this.data,
      /*{jianchacheshen,cheshenzhaopian,jianchahoushijing,houshijingzhaopian,
        jianchamiehuoqi,miehuoqizhaopian,kecheanquanchui,anquanchuizhaopian,
        jianchaanquandai,anquandaizhaopian,jianchaguashuiqi,jianchafadongji,
        jianchalingqueye,jianchazhidongxitong,zhidongxitongzhaopian,jianchazhidongye,
        zhidongyezhaopian,jianchaxingchezhidong,jianchazhuchezhidong,jianchaluntai,
        qianzuoluntaizhaopian,qianyouluntaizhaopian,zhongqiaozuozhuluntaizhaopian,zhongqiaozuobianluntaizhaopian,
        zhongqiaoyouzhuluntaizhaopian,zhongqiaoyoubianluntaizhaopian,houqiaozuoluntaizhaopian,houqiaoyouluntaizhaopian,
        jianchachelunluoshuanluomu,jianchaqianzhaodeng,jianchaxinhaozhishi,jianchayibiao
      } = fieldsObj,*/
      tableLayout = [];
    if (type === 'e_cheliangyirisanjianbiao'){
      pageTitle = '出车前检查';
      tableLayout = [
        {
          title: '车辆外观及附属设施',
          fieldArr: ["jianchacheshen","cheshenzhaopian","jianchahoushijing","houshijingzhaopian",
            "jianchamiehuoqi","miehuoqizhaopian","kecheanquanchui","anquanchuizhaopian",
            "jianchaanquandai","anquandaizhaopian","jianchaguashuiqi"]
        },
        {
          title: '发动机',
          fieldArr: ["jianchafadongji", "jianchalingqueye"]
        },
        {
          title: '制动',
          fieldArr: ["jianchazhidongxitong","zhidongxitongzhaopian","jianchazhidongye",
            "zhidongyezhaopian","jianchaxingchezhidong","jianchazhuchezhidong",]
        },
        {
          title: '车轮及轮胎',
          fieldArr: ["jianchaluntai",
            "qianzuoluntaizhaopian","qianyouluntaizhaopian","zhongqiaozuozhuluntaizhaopian","zhongqiaozuobianluntaizhaopian",
            "zhongqiaoyouzhuluntaizhaopian","zhongqiaoyoubianluntaizhaopian","houqiaozuoluntaizhaopian","houqiaoyouluntaizhaopian",
            "jianchachelunluoshuanluomu"]
        },
        {
          title: '照明、信号指示装置及仪表',
          fieldArr: ["jianchaqianzhaodeng","jianchaxinhaozhishi","jianchayibiao"]
        },
      ]
    }
    if (type === 'e_cheliangyirisanjianbiao_driving'){
      pageTitle = '行车中检查';
      tableLayout = [
        {
          title: '车轮及轮胎',
          fieldArr: ["jianchaluntai",
            "qianzuoluntaizhaopian","qianyouluntaizhaopian","zhongqiaozuozhuluntaizhaopian","zhongqiaozuobianluntaizhaopian",
            "zhongqiaoyouzhuluntaizhaopian","zhongqiaoyoubianluntaizhaopian","houqiaozuoluntaizhaopian","houqiaoyouluntaizhaopian",
            "jianchachelunluoshuanluomu"]
        },
        {
          title: '照明、信号指示装置及仪表',
          fieldArr: ["jianchayibiao"]
        },
      ]
    }
    if (type === 'e_cheliangyirisanjianbiao_after'){
      pageTitle = '收车后检查';
      tableLayout = [
        {
          title: '车辆外观及附属设施',
          fieldArr: ["jianchacheshen","cheshenzhaopian",
            "jianchamiehuoqi","miehuoqizhaopian","kecheanquanchui","anquanchuizhaopian",
            "jianchaanquandai","anquandaizhaopian",]
        },
      ]
    }
    console.log(this.data.rid);
    if (this.data.rid) {
      tableLayout.push({title: '',fieldArr: ['qianming']})
    }
    this.setData({['formData.jianchaleixing']: pageTitle,pageTitle})
    // this.data.formData.jianchaleixing = pageTitle;
    //获取字段信息
    this.getCustomField().then(data =>{
      tableLayout.forEach((item,index) => {
        let arr = [];
        item.fieldArr.forEach(ite =>{
          let field = data.find(it => it.field_id === ite);
          if (field.type === 'picture'){
            // field.is_take_a_photo = true;
            field.compress = true;
            field.watermark = () => this.getWatermark();
          }
          arr.push(field)
        });
        item.fields = arr;
        this.setData({[`tableLayout[${index}]`]: item});
      });

    })
  },

  //获取模块字段
  getCustomField(){
    return new Promise(resolve => {
      let {entity} = this.data,
        url = 'QX/PRO_select_field',
        post = {"table": entity,"fieldname":""};
      wx.showLoading({mask: true})
      wx.$post(4,url,post).then(res => {
        if (res.errcode != 0) return;
        wx.hideLoading();
        let data = res.data.row || [];
        resolve(data)
      })
    })
  },

  //收起选择框
  closeSelect(e){
    let {id} = e.currentTarget.dataset,
      {formData} = this.data,
      obj = { [`show.${id}`]: false};
    if (formData[id] === '隐患') obj[`formData.${id}`] = null
    this.setData(obj)
  },
  //检查项目选择操作
  bindSelect(e){
    //查看界面时不可编辑
    if (this.data.rid) return;
    let {id,type,index} = e.currentTarget.dataset,
      val = '隐患',
      obj = {};
    if (type === 'normal') {
      val = '正常'
    }else {
      obj[`show.${id}`] = true
    }
    obj[`formData.${id}`] = val;
    this.setData({isEdit: true})
    this.setData(obj);
    this.setData({isEdit: false})
  },

  //上传图片增加水印
  //初始化数据
  initData(){
    return new Promise(resolve => {
      let {status} = this.data,
        target = status === 0 ? 'start' : 'end';

      let nowTime = wx.$util.formatDate(new Date(),'yyyy-MM-dd HH:mm');
      this.setData({
        nowTime,
      });
      wx.showLoading();
      this.wxLocation().then(res => {
        wx.hideLoading();
        let {address,location,formatted_addresses} = res.result;
        address = formatted_addresses && formatted_addresses.recommend || address;
        this.setData({
          address,location,
        })
        resolve();
      },() =>{
        wx.hideLoading();
        wx.showModal({
          title: '定位服务未打开',
          content: '请在系统设置中打开位置授权，以便我们为您提供更好的服务',
          confirmText: '确定',
          showCancel: false,
          success: (res) => {
            if(res.confirm) {
              wx.navigateTo({url:'/SlagCar/OpenLocationGuide/OpenLocationGuide'})
            }
          },
        });
      })

    })
  },
  //  获取当前位置
  wxLocation(){
    return new Promise((resolve,reject) => {
      // resolve({latitude:23.035508897569443 , longitude:113.11290771484374});
      //231227 yan 修改获取定位方法
      /*wx.getLocation({
        type: 'gcj02', // 比较精确
        success: (res) => {
          this.mapGeocoder(res).then(result => {
            resolve(result);
          })
        },
        fail: (res) =>{
          console.log(res);
          reject();
        }
      })*/

      wx.$getLocation().then(res =>{
        this.mapGeocoder(res).then(result => {
          resolve(result);
        })
      },reject)
    })
  },

  //腾讯地图api逆地址转换
  mapGeocoder(latlng){
    let url = 'map/tencent/ws/geocoder/location';
    let{latitude, longitude} = latlng;
    return new Promise(resolve => {
      wx.$post(2,url,{
        "key": config.mapKey,
        "location": `${latitude},${longitude}`,
        "get_poi": "",
        "poi_options": "",
        "output": "",
        "callback": ""
      },false,true).then(res =>{
        resolve(res)
      })
    })
  },

  //获取水印信息
  getWatermark(){
    return new Promise(resolve => {
      this.initData().then(() => {

        let {address,driver_car_name,nowTime} = this.data;
        let arr = [{
          content: address,
          font: '14px serif',
          position: {
            bottom: 20,
            left: 15
          }
        },
          {
            content: driver_car_name,
            font: '24px serif',
            position: {
              bottom: 50,
              left: 15
            }
          },
          {
            content: nowTime,
            font: '25px serif',
            position: {
              bottom: 80,
              left: 15
            }
          },
          /*{
            content: 'date',
            font: '26px serif',
            position: {
              bottom: 120,
              left: 30
            }
          },*/];
        resolve(arr)
      });
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let {rid,type,entity} = options,
      obj = {};
    this.setData({entity,rid})
    this.prepare(entity);
    if (rid) {
      obj.readonly = true;  //只读状态
      obj.rid = rid;
      obj.isEdit = true;  //查看状态
      this.getRowData(rid);
    }else {
      obj.driver_car_name = wx.getStorageSync('driver_car_name');
    }
    let driver_car_row_id = wx.getStorageSync('driver_car_row_id');
    /*let defFields = ['sjmzhuangkuang','mhqzhuangkuang','xszyyzzhuangkuang','wxbjzhuangkuang','zdxtzhuangkuang','qcdgzhuangkuang','ltzhuangkuang','zxxtzhuangkuang','zcjlyzhuangkuang','gpszhuangkuang','znspjkzhuangkuang',],
      defData = {};
      defFields.forEach(item => {
        defData[item] = '正常'
    });
      obj.rowData = defData;*/

    obj.formData = {...this.data.formData,chepaihaomayinyong: driver_car_row_id,chuchebiaoji: '已出车'};
    this.setData(obj);

    // this.data.formData.chepaihaomayinyong = driver_car_row_id;
    // this.data.formData.chuchebiaoji = '已出车';
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    wx.getSetting({
      success(res){
        if (!res.authSetting['scope.userLocation']){
          console.log('未打开定位服务');
          wx.openSetting();
          wx.getLocation({
            type: 'gcj02', // 比较精确
            success: (res) => {
              console.log(res);
            },
            fail: (res) =>{
              wx.showModal({
                title: '定位服务未打开',
                content: '请在系统设置中打开位置授权，以便我们为您提供更好的服务',
                confirmText: '去开启',
                success: (res) => {
                  if(res.confirm) {
                    wx.openSetting();
                  }
                },
              });
            }
          })

        }else {
          const systemSetting = wx.getSystemSetting();
          if (systemSetting.locationEnabled === false) {
            wx.showModal({
              title: '定位服务未打开',
              content: '请在系统设置中打开位置授权，以便我们为您提供更好的服务',
              confirmText: '确定',
              showCancel: false,
              success: (res) => {
                if(res.confirm) {
                  wx.navigateTo({url:'/SlagCar/OpenLocationGuide/OpenLocationGuide'})
                }
              },
            })
          }
        }

      }
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})
